package org.tarena.dang.action.main;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.List;

import org.tarena.dang.action.BaseAction;
import org.tarena.dang.entity.Book;
import org.tarena.dang.entity.Categrory;
import org.tarena.dang.service.BookService;
import org.tarena.dang.service.BookServiceImp;
import org.tarena.dang.service.CategoryService;
import org.tarena.dang.service.CategoryServiceImp;

public class SearchAction extends BaseAction {
	private String keyWords;
	private List<Book> books;
	private List<Categrory> cats;
	private int totalNum;//全部记录数
	private int cid;
	public int getCid() {
		return cid;
	}

	public void setCid(int cid) {
		this.cid = cid;
	}

	public int getTotalNum() {
		return totalNum;
	}

	public void setTotalNum(int totalNum) {
		this.totalNum = totalNum;
	}

	public String execute(){
		BookService bookService = new BookServiceImp();
		CategoryService categoryService = new CategoryServiceImp();
		try {
			
				keyWords = new String(keyWords.getBytes("iso-8859-1"),"utf-8");
			
			
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		System.out.println("搜索关键字----"+keyWords);
		try {
			//books = bookService.searchByKeyWords(keyWords);
			books = bookService.searchByKeyWords(keyWords,cid);
			cats = categoryService.searchByKeyWords(keyWords);
			for(Categrory c:cats){
				totalNum+=c.getPnum();
			}
			System.out.println(books.size());
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return "success";
	}
	
	public String getKeyWords() {
		return keyWords;
	}
	public void setKeyWords(String keyWords) {
		this.keyWords = keyWords;
	}
	public List<Book> getBooks() {
		return books;
	}
	public void setBooks(List<Book> books) {
		this.books = books;
	}

	public List<Categrory> getCats() {
		return cats;
	}

	public void setCats(List<Categrory> cats) {
		this.cats = cats;
	}
	
}
